<?php
/**
 * Created by PhpStorm.
 * User: zhengkunming
 * Date: 2018/12/26
 * Time: 2:31 PM
 */

require_once 'mysql_config.php';

$openid = isset($_POST['openid']) ? $_POST['openid'] : '';


if ('' === $openid) {
    $data = array(
        'code' => -1,
        'data' => array(),
        'msg' => 'require openid'
    );
    rtJson($data);
    return;
}

$query_user = 'SELECT * FROM users WHERE openid = "' . $openid . '"';

$query_user_score = 'SELECT
	T_user_score.openid,
	SUM( T_user_score.score ) score 
FROM
	(
	SELECT
		openid,
		score 
	FROM
		users 
	WHERE
		1 = 1 UNION ALL
	SELECT
		openid,
		score 
	FROM
		check_in 
	WHERE
		1 = 1 UNION ALL
	SELECT
		openid,
		score 
	FROM
		answer_score 
	WHERE
		1 = 1 
	) T_user_score 
WHERE
	T_user_score.openid = "' . $openid . '";';


$query_shop_score = 'SELECT
	shop,
	SUM( score ) shop_score 
FROM
	(
	SELECT
		shop,
		score 
	FROM
		users 
	WHERE
		1 = 1 
		AND created >= ( SELECT DATE_ADD( CURDATE( ), INTERVAL - DAY ( CURDATE( ) ) + 1 DAY ) ) 
		AND created <= ( SELECT LAST_DAY( CURDATE( ) ) ) UNION ALL SELECT shop, score FROM check_in WHERE 1 = 1 AND created >= ( SELECT DATE_ADD( CURDATE( ), INTERVAL - DAY ( CURDATE( ) ) + 1 DAY ) ) 
		AND created <= ( SELECT LAST_DAY( CURDATE( ) ) ) UNION ALL SELECT shop, score FROM answer_score WHERE 1 = 1 AND created >= ( SELECT DATE_ADD( CURDATE( ), INTERVAL - DAY ( CURDATE( ) ) + 1 DAY ) ) 
		AND created <= ( SELECT LAST_DAY( CURDATE( ) ) ) 
	) T_shop_score 
WHERE
	shop IN ( SELECT shop FROM users WHERE openid = "' . $openid . '" ) 
GROUP BY
	T_shop_score.shop;';

$query_res_user = mysqli_query($link, $query_user);
$query_res_user_score = mysqli_query($link, $query_user_score);
$query_res_shop_score = mysqli_query($link, $query_shop_score);


if (FALSE === $query_res_user) {
    $data = array(
        'code' => -3,
        'data' => array(),
        'msg' => 'database err'
    );
    rtJson($data);
    return;
}

$query_num_rows = $query_res_user->num_rows;

if (0 === $query_num_rows) {
    $data = array(
        'code' => -2,
        'data' => array(),
        'msg' => 'not user'
    );
    rtJson($data);
    return;
} else {
    $user = $query_res_user->fetch_object();
    $get_user_score = $query_res_user_score->fetch_object();
    if (0 === $query_res_shop_score->num_rows) {
        $shop_score = 0;

    } else {
        $get_shop_score = $query_res_shop_score->fetch_object();
        $shop_score = $get_shop_score->shop_score;
    }


//    echo $user_shop_score->shop_score;
//    echo ' <br>';
//    echo $check_shop_score->shop_score;
//    echo ' <br>';
//    echo $answer_shop_score->shop_score;
//    return;
    $data = array(
        'code' => 0,
        'data' => array(
            'openid' => $user->openid,
            'full_name' => $user->full_name,
            'mobile' => $user->mobile,
            'shop' => $user->shop,
            'score' => intval($get_user_score->score),
            'shop_score' => intval($shop_score)
        ),
        'msg' => ''
    );
    rtJson($data);
    return;
}
